/**
 * async  aswit     Future Stream
 * Future代表一个异步执行的操作(数据)，而Stream表示一连串异步操作（数据）。
 * 异步操作返回的可能是正常的数据，也可能是错误。
 */

import 'dart:async';

Future<String> fetchData() async {
  await Future.delayed(Duration(seconds: 2));
  return "Data fetched!";
}

Future<void> performOperations() async {
  print('Fetching data...');
  try {
    String data = await fetchData();
    print(data); // 输出: Data fetched!
  } catch (e) {
    print('Error fetching data: $e');
  }

  print('Starting parallel operations...');
  var futures = [fetchData(), fetchData(), fetchData()];
  List results = await Future.wait(futures);
  print('All operations completed!');
  for (var result in results) {
    print(result);
  }
}

void main() async {
  await performOperations();
}